//
//  NSString+wtfssd.m
//  WhereToEat
//
//  Created by WTFSSD on 2017/6/2.
//  Copyright © 2017年 WTFSSD. All rights reserved.
//

#import "NSString+wtfssd.h"

#import <UIKit/UIKit.h>

@implementation NSString (wtfssd)


-(NSString *(^)(id))add{
    return ^(id obj){
        return [NSString stringWithFormat:@"%@%@",self,obj];
        
    };
}


-(BOOL (^)(id))eq{
    return ^(id obj){
        return [self isEqualToString:obj];
    };
}

-(NSString *(^)(void))upcase{
    return ^(){
        return [self uppercaseString];
    };
}


-(NSString *(^)(void))lowercase{
    return ^(){
        return [self lowercaseString];
    };
    
}


-(NSURL *(^)(void))toUrl{
    return ^(){
        return [NSURL URLWithString:self];
    };
}

-(CGSize (^)(UIFont *))sizeOfFont{
    return ^(UIFont * font){
        NSDictionary*attrs =@{NSFontAttributeName: font};
        return [self boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
    };
}

-(NSDictionary *(^)())getQuery{
    __block NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    return ^(){
        for (NSString * s in [self componentsSeparatedByString:@"&"]) {
            NSArray * arr1 = [s componentsSeparatedByString:@"="];
            [dic setValue:arr1.lastObject forKey:arr1.firstObject];
        }
        return [NSDictionary dictionaryWithDictionary:dic];
    };
}

-(BOOL (^)(NSString *, BOOL))contain{
    return ^(NSString * s,BOOL caps){
        if (caps) {
            return [self containsString:s];
        }else{
            return [self.upcase() containsString:s.upcase()];
        }
    };
}
- (NSString *)firstCharactor
{
    //转成了可变字符串
    NSMutableString *str = [NSMutableString stringWithString:self];
    //先转换为带声调的拼音
    CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformMandarinLatin,NO);
    //再转换为不带声调的拼音
    CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformStripDiacritics,NO);
    //转化为大写拼音
    NSString *pinYin = [str capitalizedString];
    //获取并返回首字母
    return [pinYin substringToIndex:1];
}
@end
